<template>
  <div class="product-list">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="出售中" name="sell">
          <span slot="label">出售中 <el-tag size="mini">{{product_count.sell}}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="仓库中" name="lower">
          <span slot="label">仓库中 <el-tag size="mini">{{product_count.lower}}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="回收站" name="recovery">
          <span slot="label">回收站 <el-tag size="mini">{{product_count.recovery}}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="库存紧张" name="stock">
          <span slot="label">库存紧张 <el-tag size="mini">{{product_count.stock}}</el-tag></span>
        </el-tab-pane>
        <el-tab-pane label="已售罄" name="over">
          <span slot="label">已售罄 <el-tag size="mini">{{product_count.over}}</el-tag></span>
        </el-tab-pane>
      </el-tabs>
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="商品分类">
          <el-select size="small" v-model="searchForm.category_id" placeholder="所有分类">
            <el-option label="全部" value="0"></el-option>
            <el-option v-for="(item, index) in categoryList" :key="index" :label="item.name" :value="item.category_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input size="small" v-model="searchForm.product_name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品编码">
          <el-input size="small" v-model="searchForm.product_no" placeholder="请输入商品编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--添加产品-->
    <div class="common-level-rail"><el-button size="small" type="primary" icon="el-icon-plus" @click="addClick" v-auth="'/product/product/add'">添加产品</el-button></div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="product_name" label="产品" width="400px">
            <template slot-scope="scope">
              <div class="product-info">
                <div class="pic"><img v-img-url="scope.row.image[0].file_path" alt="" /></div>
                <div class="info">
                  <div class="name">{{ scope.row.product_name }}</div>
                  <div class="price">￥{{ scope.row.product_price }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="category.name" label="分类"></el-table-column>
          <el-table-column prop="sales_actual" label="实际销量"></el-table-column>
          <el-table-column prop="product_stock" label="库存"></el-table-column>
          <el-table-column prop="view_times" label="点击数"></el-table-column>
          <el-table-column prop="product_status.text" label="商品属性">
            <template slot-scope="scope">
              <span v-if="scope.row.is_virtual == 0">实物商品</span>
              <span v-if="scope.row.is_virtual == 1" class="green">虚拟商品</span>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="发布时间"></el-table-column>
          <el-table-column prop="product_sort" label="排序"></el-table-column>
					<el-table-column v-if="activeName == 'sell' || activeName == 'lower'" :label="activeName == 'sell' ? '下架定时时间' : '上架定时时间'">	<!--{{ activeName == 'sell' ? '下架定时时间' : '上架定时时间' }}-->
						<template slot-scope="{row}">
							<span v-if="row.timer && row.timer.publish_time > 0">{{ timeFilter(row.timer.publish_time) }}</span>
						</template>
					</el-table-column>
          <el-table-column fixed="right" label="操作" width="270">
            <template slot-scope="scope">
              <el-button @click="editClick(scope.row)" type="text" size="small" v-auth="'/product/product/edit'">编辑</el-button>
              <el-button v-if="activeName == 'sell'" @click="changeStatus(scope.row, 20)" type="text" size="small" v-auth="'/product/product/edit'">下架</el-button>
              <el-button v-if="activeName == 'lower'" @click="changeStatus(scope.row, 10)" type="text" size="small" v-auth="'/product/product/edit'">上架</el-button>
              <el-button v-if="activeName == 'recovery'" @click="changeStatus(scope.row, 20)" type="text" size="small" v-auth="'/product/product/edit'">恢复</el-button>
              <el-button v-if="activeName == 'sell'" @click="changeTiming(scope.row, 20)" type="text" size="small" v-auth="'/product/product/edit'">下架定时</el-button>
              <el-button v-if="activeName == 'lower'" @click="changeTiming(scope.row, 10)" type="text" size="small" v-auth="'/product/product/edit'">上架定时</el-button>
              <el-button @click="delClick(scope.row)" type="text" size="small" v-auth="'/product/product/delete'">删除</el-button>
              <el-button @click="copyClick(scope.row)" type="text" size="small" v-auth="'/product/product/add'">复制</el-button>
              <el-button @click="linkClick(scope.row.product_id)" type="text" size="small">链接</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>

    <el-dialog title="商品定时" :visible.sync="dialogTimer" v-if="product != null" width="350px" :before-close="closeTimer" v-loading="loadingTimer">
      <el-form :model="formTimer">
        <el-form-item label="是否定时" label-width="80px">
          <el-switch v-model="formTimer.whether" active-value="100" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="定时时间" label-width="80px">
          <el-date-picker v-model="formTimer.solt" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间" :default-time="formTimer.defaultTime" autoComplete="false">
        </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="normal"  @click="closeTimer">取消</el-button>
        <el-button size="mini" type="primary" @click="changeStatusTiming">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
import { compareTime } from '@/utils/DateTime.js'
export default {
  components: {},
  data() {
    return {
      /*切换菜单*/
      activeName: 'sell',
      /*切换选中值*/
      activeIndex: '0',
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*搜索参数*/
      searchForm: {
        product_name: '',
        product_no:'',
        category_id: ''
      },
      /*列表数据*/
      tableData: [],
      /*全部分类*/
      categoryList: [],
      product_count: {},
       message: 'pages/product/detail/detail?product_id=',

      dialogTimer: false,
      formTimer: {
        whether: '',
        solt: null,
        defaultTime: '',
      },
      product: null,
      state: 0,
      loadingTimer: false,
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },

    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },

    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.type = self.activeName;
      self.loading = true;
      PorductApi.productList(Params, true)
        .then(res => {
          self.loading = false;
          self.tableData = res.data.list.data;
          self.categoryList = res.data.category;
          self.totalDataNumber = res.data.list.total;
          self.product_count = res.data.product_count;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },

    /*打开添加*/
    addClick() {
      this.$router.push('/product/product/add');
    },

    /*打开编辑*/
    editClick(row) {
      this.$router.push({
        path: '/product/product/edit',
        query: {
          product_id: row.product_id,
          scene: 'edit'
        }
      });
    },
    dataProcessing(val) {
      let id = val + '';
      this.message = this.message + id;
    },

    linkClick: function(val) {
      let self = this;
      self.dataProcessing(val)
      console.log(self.message)
      self.$copyText(self.message).then(function(e) {
        self.$message({
          showClose: true,
          message: '复制成功',
          type: 'success'
        });
      }, function(e) {
        self.$message({
          showClose: true,
          message: '复制失败',
          type: 'error'
        });
      })
    },
   /*打开复制*/
     copyClick(row) {

       this.$router.push({
         path: '/product/product/edit',
         query: {
           product_id: row.product_id,
           scene: 'copy'
         }
       });
     },
    /*删除*/
    delClick: function(row) {
      let self = this;
      let msg = '';
      if(row.product_status == 30){
        msg = '删除后不可恢复，确认删除该记录吗?';
      }else{
        msg = '删除后将移动到回收站，确定删除该记录吗?';
      }
      self
        .$confirm(msg, '提示', {
          type: 'warning'
        })
        .then(() => {
          PorductApi.delProduct({
            product_id: row.product_id
          }).then(data => {
            self.$message({
              message: '删除成功',
              type: 'success'
            });
            self.getData();
          });
        });
    },
    /*修改状态*/
    changeStatus(item,value){
      let self = this;
      let msg = '';
      if(value == 10){
        msg = '确认要上架该商品吗?';
      }else{
        msg = '确认要下架该商品吗?';
        if(self.activeName == 'recovery'){
          msg = '确认要移动该商品到仓库吗?';
        }
      }
      self.$confirm(msg, '提示', {
          type: 'warning'
        })
        .then(() => {
          PorductApi.changeStatus({
            product_id: item.product_id,
            state: value
          }).then(data => {
            self.$message({
              message: '操作成功',
              type: 'success'
            });
            self.getData();
          });
        });
    },
    changeTiming(item, value) {
      this.product = item
      this.state = value
      // 编辑定时
      if (item.timer && item.timer.publish_time > 0) {
        this.formTimer = {
          whether: '100',
          solt: this.timeFilter(item.timer.publish_time),
        }
      }
      // 定时器默认时间
      this.formTimer.defaultTime = value == 10 ? '10:00:00' : '23:59:59'

      this.dialogTimer = true
    },
    closeTimer() {
      this.product = null
      this.state = 0
      // 定时器默认时间
      this.formTimer.whether = ''
      this.formTimer.solt = null
      this.formTimer.defaultTime = ''
      this.dialogTimer = false
    },
    // 定时上下架
    changeStatusTiming() {
      // 如果开启定时上下架,必须选择时间
      if ((this.formTimer.whether == 100) && !this.formTimer.solt) {
        this.$message({
          message: '开启定时必须选择时间',
          type: 'error'
        });
        return false;
      }

      // 关闭定时
      if (this.formTimer.whether == 0) {
        this.deleteTiming(this.product.product_id, this.state)
      } else {
        let self = this;
        let msg = '';
        if (this.state == 10) {
          msg = '确认要定时上架该商品吗?';
        }else{
          msg = '确认要定时下架该商品吗?';
        }

        // 时间限制
        if (compareTime(this.formTimer.solt) < 60) {
          this.$message({
            message: '定时时间距离现在必须大于1分钟',
            type: 'error'
          });
          return false;
        }

        self.$confirm(msg, '提示', {
          type: 'warning'
        }).then(() => {
          self.loadingTimer = true
          PorductApi.changeStatusTimer({
            product_id: this.product.product_id,
            state: this.state,
            solt: this.formTimer.solt,
          }).then(() => {
            self.$message({
              message: '操作成功',
              type: 'success'
            });

            self.loadingTimer = false
            self.dialogTimer = false
            self.closeTimer()
            self.getData();
          });
        }).catch(err => {
          self.loadingTimer = false
          self.$message({
            message: err,
            type: 'error'
          });
        });

        self.loadingTimer = false
        self.loadingTimer = false
      }
    },
    // 删除定时上下架
    deleteTiming(product_id, state) {
      let tips = state == 10 ? '确认要关闭该商品的上架定时吗?' : '确认要关闭该商品的下架定时吗?'
      this.$confirm(tips, '提示', {
        type: 'warning'
      }).then(() => {
        this.loadingTimer = true
        PorductApi.deleteTimer({product_id: product_id}).then(() => {
          this.$message({
            message: '操作成功',
            type: 'success'
          });

          this.loadingTimer = false
          this.dialogTimer = false
          this.getData();
        });
      }).catch(err => {
        this.loadingTimer = false
        this.$message({
          message: err,
          type: 'error'
        });
      });

      this.loadingTimer = false
      this.dialogTimer = false
    },
    // 格式化时间
    timeFilter(time) {
      return this.$filter.parseTime(time)
    },
    // 修改定时上下架
    editTiming(product, state) {
      this.product = product
      this.state = state

      // 定时器默认时间
      this.formTimer.whether = ''
      this.formTimer.solt = ''
      this.formTimer = {
        whether: '100',
        solt: this.timeFilter(product.timer.publish_time),
        defaultTime: state == 10 ? '10:00:00' : '23:59:59'
      }
      this.dialogTimer = true
    },
  }
};
</script>

<style>

</style>
