<template>
  <div class="app-container">
        <div>
          <el-select
            v-model="listQuery.type"
            placeholder="标准状态"
            clearable
            style="width: 200px"
            class="filter-item"
            @change="handleFilter"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.display_name"
              :value="item.key"
            />
          </el-select>
          <el-input
            v-model="listQuery.search"
            placeholder="编号/名称"
            style="width: 300px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >搜索</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-refresh-left"
            @click="resetFilter"
          >刷新重置</el-button>
        </div>
        <div style="margin-top:6px">
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="standardList.results"
          style="width: 100%;margin-top:10px;"
          border
          fit
          stripe
          highlight-current-row
          max-height="600"
        >
          <el-table-column type="index" width="50" />
          <el-table-column label="标准编号">
            <template slot-scope="scope">{{ scope.row.code }}</template>
          </el-table-column>
          <el-table-column label="标准名称">
            <template slot-scope="scope">
                <el-link type="primary" :href="scope.row.path" target="_blank" v-if="scope.row.path">{{ scope.row.name }}</el-link>
                <span v-else>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="header-center" label="发布日期">
            <template slot-scope="scope">{{ scope.row.publish_date }}</template>
          </el-table-column>
          <el-table-column align="header-center" label="实施日期">
            <template slot-scope="scope">{{ scope.row.implement_date }}</template>
          </el-table-column>
          <el-table-column align="header-center" label="状态">
            <template slot-scope="scope">{{ scope.row.status }}</template>
          </el-table-column>
                <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            :disabled="!checkPermission(['standard_update'])"
            @click="handleUpdate(scope)"
          />
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            :disabled="!checkPermission(['standard_delete'])"
            @click="handleDelete(scope)"
          />
        </template>
      </el-table-column>
        </el-table>
        <pagination
          v-show="standardList.count>0"
          :total="standardList.count"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='update'?'编辑标准':'新增标准'">
      <el-form
        ref="Form"
        :model="standard"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
      <el-form-item label="状态" prop="status">
          <el-select
            v-model="standard.status"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.display_name"
              :value="item.key"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="编号" prop="code">
          <el-input v-model="standard.code" placeholder="编号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="standard.name" placeholder="名称" />
        </el-form-item>
        <el-form-item label="发布日期" prop="publish_date">
          <el-date-picker
      v-model="standard.publish_date"
      value-format="yyyy-MM-dd"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
        </el-form-item>
        <el-form-item label="实施日期" prop="implement_date">
          <el-date-picker
      
      v-model="standard.implement_date"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
        </el-form-item>
        <el-form-item label="文件" prop="path">
          <el-upload
  
  :on-preview="handlePreview"
  :on-success="handleSuccess"
  :action="upUrl"
  :headers="upHeaders"
  :limit="1"
  :file-list="fileList"
  >
  <el-button size="small" type="primary" >点击上传</el-button>
  
</el-upload>
        </el-form-item>
      </el-form>
      
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getStandardList, createStandard, updateStandard, deleteStandard } from "@/api/standard"
import { upUrl, upHeaders} from "@/api/file"
import Pagination from "@/components/Pagination"
import checkPermission from '@/utils/permission'
const defaultStandard = {
  id:null,
  code:null,
  name:null,
  publish_date:null,
  implement_date:null,
  status:'现行'
}
export default {
  components: { Pagination },
  data() {
    return {
      upHeaders: upHeaders(),
      upUrl: upUrl(),
      standardList: {count:0},
      standard:Object.assign({}, defaultStandard),
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20
      },
      statusOptions: [
        { key: "现行", display_name: "现行" },
        { key: "即将实施", display_name: "即将实施" },
        { key: "作废", display_name: "作废" },
        { key: "废止", display_name: "废止" }
      ],
      dialogVisible:false,
      dialogType:'create',
      rule1:{
        code: [{ required: true, message: "请输入编号", trigger: "blur" }],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        publish_date: [{ required: true, message: "请输入发布日期", trigger: "change" }],
        implement_date: [{ required: true, message: "请输入实施日期", trigger: "change" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }]
      },
      fileList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getStandardList(this.listQuery).then(response => {
        if (response.data) {
          this.standardList = response.data
        }
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20
      };
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCreate() {
      this.standard = Object.assign({}, defaultStandard);
      this.fileList = []
      this.dialogType = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleUpdate(scope) {
      this.standard = Object.assign({}, scope.row) // copy obj
      if(this.standard.path){
        this.fileList=[{
          name:this.standard.code + this.standard.name,
          url:this.standard.path
        }]
      }
      this.dialogType = 'update'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['Form'].clearValidate()
      })
    },
    handlePreview(file) {
      if ('url' in file){
        window.open(file.url)
      }else{
        window.open(file.response.data.path)
      }
      
    },
    handleSuccess(response, file, fileList) {
      this.standard.path = response.data.path
    },
    async confirm(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateStandard(this.standard.id, this.standard).then(res => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false
                this.$message.success('成功')
              }
            });
          } else {
            createStandard(this.standard).then(res => {
                this.getList();
                this.dialogVisible = false
                this.$message.success('成功')
              
            }).catch(error=>{})
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>
